﻿using System;
using System.Collections.Generic;
using System.Linq;
using nelech.Models.DBModel;
using System.Web;
using nelech.Controllers.Utils;
using System.Web.Mvc;
using System.IO;

namespace nelech.Controllers
{
    public class KuponController : Controller
    {

        nelechDBEntities db = new nelechDBEntities();
        public ActionResult Index(int? id)
        {
            var kupons = db.Kupon.Where(p => p.publish.Value);
            foreach (var kupon in kupons)
            {
                kupon.RegeonReference.Load();
            }
            if (id == null)
            {
                return View(kupons);
            }
            else
            {

                return View(kupons.Where(p => p.Regeon.id == id));
            }
        }

        public ActionResult KuponInfo(int id)
        {
            var kupon = db.Kupon.First(p => p.KuponId == id);
            kupon.RegeonReference.Load();
            return View(kupon);
        }

        [Authorize(Users="admin,editor")]
        public ActionResult CreateKupon()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult CreateKupon(Kupon kupon, int region)
        {
            if (!ModelState.IsValid)
                return View();

            db.AddToKupon(kupon);
            kupon.Regeon = db.Regeon.First(p => p.id == region);
            AddKuponImages(kupon);
            kupon.publish = true;
            db.SaveChanges();

            return RedirectToAction("ListAdminKupon", "Kupon");
        }

        [Authorize(Users="admin,editor")]
        public ActionResult EditKupon(int id)
        {
            var kupon = db.Kupon.First(p => p.KuponId == id);
            kupon.RegeonReference.Load();
            return View("CreateKupon", kupon);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult EditKupon(Kupon kupon, int region)
        {
            if (!ModelState.IsValid)
                return View("CreateKupon");

            // Get the attached entity
            var k = db.Kupon.First(r => r.KuponId == kupon.KuponId);


            kupon.KuponImage = k.KuponImage;

            db.ApplyPropertyChanges(k.EntityKey.EntitySetName, kupon);


            AddKuponImages(k);

            kupon.Regeon = db.Regeon.First(p => p.id == region);
            db.SaveChanges();

            return RedirectToAction("ListAdminKupon", "Kupon");
        }


        public ActionResult ListKupon()
        {
            return View(db.Kupon.Where(p => p.publish.Value));
        }

        public ActionResult ListAdminKupon()
        {
            return View(db.Kupon.Where(p => p.publish.Value));
        }

        [Authorize(Users="admin,editor")]
        public ActionResult RecycleBinKupon()
        {
            return View(db.Kupon.Where(p => p.publish.Value));
        }

        [Authorize(Users="admin,editor")]
        public ActionResult RestoreKupon(int id)
        {
            var t = db.Kupon.First(p => p.KuponId == id);
            t.publish = true;
            db.SaveChanges();
            return RedirectToAction("RecycleBinKupon", "Kupon");
        }
        [Authorize(Users="admin,editor")]
        public ActionResult DeleteKupon(int id)
        {
            db.DeleteObject(db.Kupon.First(p => p.KuponId == id));
            db.SaveChanges();
            return RedirectToAction("RecycleBinKupon");

        }


        [Authorize(Users="admin,editor")]
        public ActionResult RecycleKupon(int id)
        {
            var t = db.Kupon.First(p => p.KuponId == id);
            t.publish = false;
            db.SaveChanges();
            return RedirectToAction("ListKupon", "Kupon");
        }

        [Authorize(Users = "admin,editor")]
        public ActionResult RemoveKuponImage(int id)
        {
            var kupon = db.Kupon.First(p => p.KuponId == id);
           
            try
            {
                System.IO.File.Delete(Server.MapPath(kupon.KuponImage));
            }
            catch (Exception e)
            {
            }
            kupon.KuponImage = "";
            db.SaveChanges();

            return Redirect(Request.UrlReferrer.ToString());
        }


        private void AddKuponImages(Kupon m)
        {
            HttpPostedFileBase KuponImage = Request.Files["KuponImage"];
            if (KuponImage != null && !string.IsNullOrEmpty(KuponImage.FileName))
            {
                var filename = SharedUtils.GetUniqueFileName(KuponImage.FileName);
                var extension = Path.GetExtension(KuponImage.FileName);
                var image = new Image { path = SharedUtils.SaveImage(KuponImage.InputStream, "/Upload/Images", filename, extension, Server, 100L, false) };
                m.KuponImage = image.path;
            }

        }
    }
}
